import 'package:dio/dio.dart';

/// [CancelToken]分组，可指定分组名称
class CancelTokenGroup extends CancelToken {
  /// 分组名称
  final String groupName;

  CancelTokenGroup({this.groupName = ""}) : super();
}

/// CancelToken提供创建CancelToken的方法
extension CancelTokenEx on CancelToken {
  static CancelToken get({String? name}) =>
      name == null ? CancelToken() : CancelTokenGroup(groupName: name);

  /// 是否是分组
  bool get isGroup => this is CancelTokenGroup;

  /// 所属分组名称，如果不是[CancelTokenGroup]则返回null
  String? get groupName =>
      isGroup ? (this as CancelTokenGroup).groupName : null;
}
